package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.domain.Room;
import com.ruoyi.basedata.domain.TerminalsXb;
import com.ruoyi.basedata.service.IRoomService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 楼栋房间Controller
 * 
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/basedata/room")
public class RoomController extends BaseController
{
    @Autowired
    private IRoomService roomService;

    /**
     * 查询楼栋房间列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(Room room)
    {
        startPage();
        List<Room> list = roomService.selectRoomList2(room);
        return getDataTable(list);
    }

    //根据楼栋查询该楼栋下的所有房间
    @PreAuthorize("@ss.hasPermi('basedata:room:list')")
    @Parameter(name = "areaCode", description = "楼栋编号", required = true)
    @GetMapping("/listByLd")
    public TableDataInfo listByLd(@RequestParam("ldId") String ldId)
    {
        List<Room> list = roomService.selectRoomListlistByLd(ldId);
        return getDataTable(list);
    }

    //根据areaCode查询房间信息
    @PreAuthorize("@ss.hasPermi('basedata:room:list')")
    @Parameter(name = "areaCode", description = "编号", required = true)
    @GetMapping("/listByAreaCode")
    public TableDataInfo listByAreaCode(@RequestParam("areaCode") String areaCode)
    {
        startPage();
        List<Room> list = roomService.selectRoomListByAreaCode(areaCode);
        return getDataTable(list);
    }

    /**
     * 导出楼栋房间列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:room:export')")
    @Log(title = "楼栋房间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Room room)
    {
        List<Room> list = roomService.selectRoomList2(room);
        ExcelUtil<Room> util = new ExcelUtil<Room>(Room.class);
        util.exportExcel(response, list, "楼栋房间数据");
    }

    /**
     * 获取楼栋房间详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:room:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(roomService.selectRoomByID(ID));
    }

    /**
     * 新增楼栋房间
     */
    @PreAuthorize("@ss.hasPermi('basedata:room:add')")
    @Log(title = "楼栋房间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Room room)
    {
        try {
            int flag = roomService.insertRoom(room);
            if (flag == 0){
                return error("所选楼栋下已有该房间");
            }
            return toAjax(flag);
        }catch (Exception e){
            return error("系统错误");
        }

    }

    /**
     * 修改楼栋房间
     */
    @PreAuthorize("@ss.hasPermi('basedata:room:edit')")
    @Log(title = "楼栋房间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Room room)
    {
        return toAjax(roomService.updateRoom(room));
    }

    //房间绑定设备
    @PreAuthorize("@ss.hasPermi('basedata:room:edit')")
    @Log(title = "楼栋房间", businessType = BusinessType.UPDATE)
    @PutMapping("/roomBoundTerminal")
    public AjaxResult roomBoundTerminal(@RequestBody Room room)
    {
        try {
            String flag = roomService.BoundTerminal(room);
            if (!flag.equals("绑定成功")){
                return error(flag);
            }
            return success(flag);
        }catch (Exception e){
            return error("系统错误");
        }

    }

    //房间绑定用户
    @PreAuthorize("@ss.hasPermi('basedata:room:edit')")
    @Log(title = "楼栋房间", businessType = BusinessType.UPDATE)
    @PutMapping("/roomBoundUser")
    public AjaxResult roomBoundUser(@RequestBody Room room)
    {
        try {
            String flag = roomService.BoundUser(room);
            if (!flag.equals("绑定成功")){
                return error(flag);
            }
            return success(flag);
        }catch (Exception e){

            return error("系统错误");
        }

    }

    //换表
    @PreAuthorize("@ss.hasPermi('basedata:room:edit')")
    @Log(title = "楼栋房间", businessType = BusinessType.UPDATE)
    @PutMapping("/changeTerminalXb")
    public AjaxResult changeTerminalXb(@RequestBody Room room)
    {
        try {
            String flag = roomService.changeTerminalXb(room);
            if (flag.equals("换表成功")){
                return success(flag);
            }
            return error(flag);
        }catch (Exception e){
            return error("系统错误");
        }

    }

    //过户
    @PreAuthorize("@ss.hasPermi('basedata:room:edit')")
    @Log(title = "楼栋房间", businessType = BusinessType.UPDATE)
    @PutMapping("/changeUser")
    public AjaxResult changeUser(@RequestBody Room room)
    {
        try {
            String flag = roomService.changeUser(room);
            if (flag.equals("过户成功")){
                return success(flag);
            }
            return error(flag);
        }catch (Exception e){
            return error("系统错误");
        }

    }

    //销表
    @PreAuthorize("@ss.hasPermi('basedata:room:edit')")
    @Log(title = "楼栋房间", businessType = BusinessType.UPDATE)
    @PutMapping("/cancelTerminal")
    public AjaxResult cancelTerminal(@RequestBody TerminalsXb terminalsXb)
    {
        try {
            boolean flag = roomService.cancelTerminal(terminalsXb);
            if (!flag){
                return error("销表失败");
            }
            return success("销表成功");
        }catch (Exception e){
            return error("系统错误");
        }

    }







    /**
     * 删除房间
     */
    @PreAuthorize("@ss.hasPermi('basedata:room:remove')")
    @Log(title = "楼栋房间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        int flag = roomService.deleteRoomByIDs(IDs);
        if (flag == 0){
            return error("该房间已绑定设备不能删除");
        }
        return toAjax(flag);
    }


    @Log(title = "房间管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Room> util = new ExcelUtil<Room>(Room.class);
        List<Room> roomList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = roomService.importData(roomList, updateSupport, operName);
        return success(message);
    }
}
